package com.slicktechnologies.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Key;
import com.slicktechnologies.client.views.contract.ContractService;
import com.slicktechnologies.shared.Contract;
import com.slicktechnologies.shared.ServiceType;

import static com.googlecode.objectify.ObjectifyService.ofy;

public class ContractServiceImplementor extends RemoteServiceServlet implements ContractService
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8606683866365273117L;

	@Override
	public void changeStatus(Contract contract) 
	{
	   
	    ServiceType serviceType=loadServiceType(contract.getServiceTypeKey());
	    String phoneNumber=contract.getCinfo().getCellNumber()+"";
	    String msg="";
	    SmsServiceImpl impl=new SmsServiceImpl();
		if(contract.getStatus().equals(Contract.SUCESSFUL))
	    {
		  if(serviceType!=null)
		  {
			msg=serviceType.getSuccessFulMessage();  
		  }
	    }
	  
	  else if(contract.getStatus().equals(Contract.CREATED))
	  {
		  msg=serviceType.getAcknowledgementMessage();
	  }
	  
	  else if(contract.getStatus().equals(Contract.UNSUCESSFUL))
	  {
		  msg=serviceType.getUnsucessFullMessage();
	  }
	  
	  else if(contract.getStatus().equals(Contract.SUCESSFUL))
	  {
		  msg=serviceType.getSuccessFulMessage();
	  }
	  
	  ofy().save().entity(contract);
	  impl.sendSms(phoneNumber, msg,"");
		
	}
	
	
   public ServiceType loadServiceType(Key<ServiceType>serviceTypeKey)
   {
	
	   if(serviceTypeKey!=null)
	   {
		  return  ofy().load().key(serviceTypeKey).now();
	   }
	   return null;
	   
   }




}
	
	
	


